package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Contract;
import com.asm.repository.ContractRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.ContractVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @param * @param null
 * @author Tanzujie
 * @date 2019-07-10
 * @return
 */
@Service
public class ContractService {
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取合同列表
     *
     * @param page
     * @return
     */
    private List<ContractVo> getContractVoList(Page<Contract> page) {
        List<ContractVo> list = new ArrayList<>();
        for (Contract Contract : page.getContent()) {
            ContractVo contractVo = new ContractVo();
            BeanUtils.copyProperties(Contract, contractVo);
            list.add(contractVo);
        }
        return list;
    }

    /**
     * 分页查询所有合同
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<ContractVo> findAllContracts(Integer pageNumber, Integer pageSize) {
        Page<Contract> page = contractRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<ContractVo>(pageNumber, pageSize, page.getTotalPages(), getContractVoList(page));
    }

    /**
     * 根据id查询合同
     *
     * @param id
     * @return
     */
    public ContractVo findContractById(Long id) {
        Contract Contract = contractRepository.findById(id).get();
        ContractVo contractVo = new ContractVo();
        BeanUtils.copyProperties(Contract, contractVo);
        return contractVo;
    }

    /**
     * 根据合同状态分页查询合同
     *
     * @param type
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<ContractVo> findContractByStatus(String status, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<Contract> contracts = contractRepository.findByStatus(status, page);
        return new PageResult<ContractVo>(pageNumber, pageSize, contracts.getTotalPages(), getContractVoList(contracts));
    }

    /**
     * 根据合同名字分页查询合同
     *
     * @param name
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<ContractVo> findContractByContractName(String name, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<Contract> contracts = contractRepository.findByContractName(name, page);
        return new PageResult<ContractVo>(pageNumber, pageSize, contracts.getTotalPages(), getContractVoList(contracts));
    }

    /**
     * 添加合同
     *
     * @param contractVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void addContract(ContractVo contractVo) {
        Contract Contract = new Contract();
        BeanUtils.copyProperties(contractVo, Contract);
        Contract.setId(snowflakeIdWorker.nextId());
        Contract.setCreateBy(1L);
        Contract.setCreateAt(new Date());
        Contract.setUpdateBy(1L);
        Contract.setUpdateAt(new Date());
        contractRepository.save(Contract);
    }

    /**
     * 更新合同
     *
     * @param contractVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateContract(ContractVo contractVo) {
        Contract Contract = contractRepository.findById(contractVo.getId()).get();
        BeanUtils.copyProperties(contractVo, Contract);
        Contract.setUpdateBy(1L);
        Contract.setUpdateAt(new Date());
        contractRepository.save(Contract);
    }

    /**
     * 根据Id删除合同
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteContract(Long id) {
        contractRepository.deleteById(id);
    }
}
